package bar.dark.lee.setting.password;

import java.lang.annotation.Annotation;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.test.UiThreadTest;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;

public class PasswordActvitiy extends SuperActivity
{
	private static final int REQUEST_PASSWORD_SETTING = 0;
	
	private CheckBox chkboxOn;
	private View viewChange;
	private View changeView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.setting_password);
		
		setTitle("비밀번호설정");
		setFindView();
		setOnClickListener();
		new ShowDialog().execute(0);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
	
	private void setFindView()
	{
		chkboxOn = (CheckBox)findViewById(R.id.settingPassword_checkbox);
		viewChange = (View)findViewById(R.id.settingPassword_change);
		changeView = findViewById(R.id.settingPasswordChangeView);
	}
	
	private void setOnClickListener()
	{
		chkboxOn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) 
			{
				if (chkboxOn.isChecked()) 
				{
					viewChange.setVisibility(View.VISIBLE);
					Intent intent = new Intent(PasswordActvitiy.this, PasswordSettingActivity.class);
					intent.putExtra("passwordRunState", PasswordSettingActivity.FIRST_RUN);
					startActivityForResult(intent, REQUEST_PASSWORD_SETTING);
				} 
				else 
				{
					SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
					SharedPreferences.Editor editor = pref.edit();
					editor.putInt(BDConstents.PASSWORD_SETTING, 0);
					editor.putString(BDConstents.PASSWORD, "");
					editor.commit();
					
					changeView.setVisibility(View.GONE);
				}
			}
		});
		
		viewChange.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(PasswordActvitiy.this, PasswordSettingActivity.class);
				intent.putExtra("passwordRunState", PasswordSettingActivity.CONFIRM_RUN);
				startActivityForResult(intent, REQUEST_PASSWORD_SETTING);
			}
		});
	}
	
	private void setData()
	{
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int passwordSetting = pref.getInt(BDConstents.PASSWORD_SETTING, 0);
		
		if (passwordSetting == 1)
		{
			chkboxOn.setChecked(true);
			
			viewChange.setVisibility(View.VISIBLE);
		}
		else
		{
			chkboxOn.setChecked(false);
			
			viewChange.setVisibility(View.GONE);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == REQUEST_PASSWORD_SETTING)
		{
			if (resultCode == Activity.RESULT_OK)
			{
				finish();
			}
			else
			{
				new ShowDialog().execute(0);
			}
		}
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(PasswordActvitiy.this);
			dialog.setCancelable(false);
			dialog.setMessage("Loading...");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			
			if (arg0[0] == 0)
			{
				return 0;
			}
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			dialog.dismiss();
			
			if (result == 0)
			{
				setData();
			}
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_PASSWORD);
		startActivity(intent);
	}
}
